﻿using System;
using Expect.Core.Fixtures;

namespace Expect.VisualStudio.Extensions.Commands.Handlers
{
    using EnvDTE;

    using EnvDTE80;

    class CreateObjectFeedFixtureClass : CreateFixtureClass
    {
        public CreateObjectFeedFixtureClass(IExpectPackage pkg)
            : base(pkg, CommandIDs.cmdidCreateObjectFeedFixtureClass)
        {
        }

        public override string DisplayText
        {
            get
            {
                return "Fixture class => create object feed fixture class";
            }
        }

        public override bool ShowCommand(SelectionResolution resolution)
        {
            var res = base.ShowCommand(resolution);
            if (res && resolution.IsFixture && resolution.Fixture.Rows.Count > 1)
            {
                for (int i = 1; i < resolution.Fixture.Rows.Count; i++)
                {
                    if (resolution.Fixture.Rows[i].Cells.Count != 2)
                        return false;
                }

                return true;
            }

            return false;
        }

        protected override Type GetClassType()
        {
            return typeof(ObjectFeedFixture);
        }

        protected override void GenerateConstructor(SelectionResolution resolution, string fixtureClassName, CodeClass generatedClass, string[] fixtureClassArgs)
        {
            var ctor = (CodeFunction2)generatedClass.AddFunction(fixtureClassName, vsCMFunction.vsCMFunctionConstructor, null, -1, vsCMAccess.vsCMAccessPublic);

            string baseParam = null;
            if (fixtureClassArgs.Length > 0)
            {
                foreach (var arg in fixtureClassArgs)
                {
                    string eltname = "param_for_" + resolution.NameProvider.ElementName(arg);
                    if (baseParam == null) baseParam = eltname;
                    ctor.AddParameter(eltname, "string", ctor.Parameters.Count);
                }
            }
            else
            {
                baseParam = "\"your object name\"";

                //edit.ReplacePattern(ctor.StartPoint, ")", "): base(\"your object name\")");
                /*
                EditPoint pt;

                if (endclass.CreateEditPoint().FindPattern(")", vsFindOptions.vsFindOptionsNone, ref pt))
                {
                    pt.
                    endclass.CreateEditPoint().Insert(": base(\"your object name\")");
                }*/
            }
            var endclass = ctor.GetStartPoint();
            var edit = ctor.StartPoint.CreateEditPoint();
            edit.EndOfLine();
            edit.Insert(" : base(" + baseParam + ")");
        }

        protected override void ProcessClass(SelectionResolution resolution, CodeClass generatedclass)
        {
            /*for (int i = 1; i < resolution.Fixture.Rows.Count; i++)
            {
                var row = resolution.Fixture.Rows[i];
                var methodname = resolution.NameProvider.ElementName(row.NamesForFluent().ToArray());
                var args = row.ArgsForFluent().ToArray();

                if (!generatedclass.HasMethodWithXArgs(methodname, args.Length))
                {
                    CreateContainerMethod.GetFixtureMethod(resolution, row, methodname, args, generatedclass);
                }
            }*/
        }
    }
}
